package com.crm.system.service.impl.csd.customerManager;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.csd.product.sceneChangeCourse;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.csd.product.*;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.productManager.CustomerMemberProductVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.service.csd.customerManager.ProductExchangeDbService;
import com.crm.system.service.csd.customerManager.ProductExchangeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author wangpeng
 * @Date 2021/9/8
 */
@Service
public class ProductExchangeServiceImpl implements ProductExchangeService {

    @Autowired
    private ProductExchangeDbService productExchangeDbService;


    /**
     * selectProductSceneList
     *
     * @Author wangpeng
     * @Date 2021/9/9
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @return
     */
    @Override
    public DhPageInfo<sceneChangeCourseVO> selectProductSceneList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder,Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<sceneChangeCourseVO> retustList=productExchangeDbService.selectProductSceneList(order.toString(),enableDisable);
        PageInfo<sceneChangeCourseVO> page =new PageInfo(retustList);
        return new DhPageInfo<sceneChangeCourseVO>(page,retustList);
    }

    /**
     * selectProductIndustryList
     *
     * @Author wangpeng
     * @Date 2021/9/9
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @return
     */
    @Override
    public DhPageInfo<industryChangeCourseVO> selectProductIndustryList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder,Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<industryChangeCourseVO> retustList=productExchangeDbService.selectProductIndustryList(order.toString(),enableDisable);
        PageInfo<industryChangeCourseVO> page =new PageInfo(retustList);
        return new DhPageInfo<industryChangeCourseVO>(page,retustList);
    }

    /**
     * selectProductHighClassList
     *
     * @Author wangpeng
     * @Date 2021/9/9
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @return
     */
    @Override
    public DhPageInfo<highClassChangeCourseVO> selectProductHighClassList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder,Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<highClassChangeCourseVO> retustList=productExchangeDbService.selectProductHighClassList(order.toString(),enableDisable);
        PageInfo<highClassChangeCourseVO> page =new PageInfo(retustList);
        return new DhPageInfo<highClassChangeCourseVO>(page,retustList);
    }

    /**
     * selectProductList
     *
     * @Author wangpeng
     * @Date 2021/9/10
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param productName
     * @return
     */
    @Override
    public DhPageInfo<CustomerProductVO> selectProductList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder,String productName) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<CustomerProductVO> retustList=productExchangeDbService.selectProductList(order.toString(),productName);
        PageInfo<CustomerProductVO> page =new PageInfo(retustList);
        return new DhPageInfo<CustomerProductVO>(page,retustList);
    }



    /**
     * addProductScene
     *
     * @Author wangpeng
     * @Date 2021/9/10
     * @param sceneParameterDays
     * @param changeCourseTitle
     * @param courseProductCode
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addProductScene(Integer sceneParameterDays, String changeCourseTitle, String courseProductCode,String finishOrderProductNum,String productType,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = null;
        sceneChangeCourseVO scene;
        Boolean whileBo = true;
        while(whileBo){
            ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
            scene= productExchangeDbService.ProductSceneList(ruleNumber);
            if(scene == null){
                whileBo = false;
            }
        }
        //新增到scene_change_course
        sceneChangeCourseVO course = new sceneChangeCourseVO();
        course.setSceneCourseId(guid);
        course.setRuleNumber(ruleNumber);
        course.setSceneParameterDays(sceneParameterDays);
        course.setChangeCourseTitle(changeCourseTitle);
        course.setCreateTm(date);
        course.setEnableDisable(1);
        course.setCourseProductCode(courseProductCode);
        course.setFinishOrderProductNum(finishOrderProductNum);
        course.setProductType(productType);
        course.setRuleTitle(ruleTitle);
        return productExchangeDbService.addProductScene(course);
    }

    /**
     * addProductIndustry
     *
     * @Author wangpeng
     * @Date 2021/9/10
     * @param industryExchangeNumber
     * @param changeCourseTitle
     * @param courseProductCode
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addProductIndustry(Integer industryExchangeNumber, String changeCourseTitle, String courseProductCode,String finishOrderProductNum,String productType,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = null;
        industryChangeCourseVO industry;
        Boolean whileBo = true;
        while(whileBo){
            ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
            industry= productExchangeDbService.ProductIndustryList(ruleNumber);
            if(industry == null){
                whileBo = false;
            }
        }
        //新增到industry_change_course
        industryChangeCourseVO course = new industryChangeCourseVO();
        course.setIndustryCourseId(guid);
        course.setRuleNumber(ruleNumber);
        course.setIndustryExchangeNumber(industryExchangeNumber);
        course.setChangeCourseTitle(changeCourseTitle);
        course.setCreateTm(date);
        course.setEnableDisable(1);
        course.setCourseProductCode(courseProductCode);
        course.setFinishOrderProductNum(finishOrderProductNum);
        course.setProductType(productType);
        course.setRuleTitle(ruleTitle);
        return productExchangeDbService.addProductIndustry(course);
    }

    /**
     * addProductHighClass
     *
     * @Author wangpeng
     * @Date 2021/9/10
     * @param productName
     * @param changeCourseTitle
     * @param productCode
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addProductHighClass(String courseProductCode,String productName, String changeCourseTitle, String productCode,String finishOrderProductNum,String courseOriginalPrice,String productType,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = null;
        highClassChangeCourseVO highClass;
        Boolean whileBo = true;
        while(whileBo){
            ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
            highClass= productExchangeDbService.ProductHighClassList(ruleNumber);
            if(highClass == null){
                whileBo = false;
            }
        }
        //新增到high_class_change_course
        highClassChangeCourseVO course = new highClassChangeCourseVO();
        course.setHighClassCourseId(guid);
        course.setRuleNumber(ruleNumber);
        course.setProductName(productName);
        course.setChangeCourseTitle(changeCourseTitle);
        course.setCreateTm(date);
        course.setEnableDisable(1);
        course.setProductCode(productCode);
        course.setCourseProductCode(courseProductCode);
        course.setFinishOrderProductNum(finishOrderProductNum);
        course.setCourseOriginalPrice(courseOriginalPrice);
        course.setProductType(productType);
        course.setRuleTitle(ruleTitle);
        return productExchangeDbService.addProductHighClass(course);
    }

    /**
     * updateEnableDisable
     * @Author wangpeng
     * @Date 2021/10/25
     * @param id   主键ID
     * @param enableDisable 1:启用  2停用
     * @param type	1：现场 2:行会；3：高端班; 4：高端班兑换现场天数;5现场天数换行业会；6行业会换现场天数
     * @return
     */
    @Override
    public Boolean updateEnableDisable(String id, String enableDisable,Integer type) {
        Boolean aBoolean = true;
        if (type == 1){
             aBoolean = productExchangeDbService.updateProductSceneEnableDisable(id, enableDisable);
        }
        if (type == 2){
            aBoolean = productExchangeDbService.updateIndustryChangeCourseEnableDisable(id, enableDisable);
        }
        if (type == 3){
            aBoolean = productExchangeDbService.updateHighClassEnableDisable(id, enableDisable);
        }
        if (type == 4){
            aBoolean = productExchangeDbService.updateHighClassExchangeDaysEnableDisable(id, enableDisable);
        }
        if (type == 5){
            aBoolean = productExchangeDbService.updateSiteExchangeIndustryEnableDisable(id, enableDisable);
        }
        if (type == 6){
            aBoolean = productExchangeDbService.updateIndustryExchangeSiteEnableDisable(id, enableDisable);
        }
        return aBoolean;
    }

    /**
     * selectChangeCourseRecord
     * @Author wangpeng
     * @Date 2021/9/26
     * @param pageIndex
 * @param pageSize
 * @param sortName
 * @param sortOrder
 * @param vo
     * @return
     */
    @Override
    public DhPageInfo<ChangeCourseRecordVO> selectChangeCourseRecord(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, ChangeCourseRecordVO vo) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<ChangeCourseRecordVO> retustList=productExchangeDbService.selectChangeCourseRecord(order.toString(),vo);
        PageInfo<ChangeCourseRecordVO> page =new PageInfo(retustList);
        return new DhPageInfo<ChangeCourseRecordVO>(page,retustList);
    }

    /**
     * selectChangeCourseRecordRuleNumber
     * 查规则编号
     * @Author wangpeng
     * @Date 2021/9/26
     * @param
     * @return
     */
    @Override
    public List<ChangeCourseRecordVO> selectChangeCourseRecordRuleNumber() {
        return productExchangeDbService.selectChangeCourseRecordRuleNumber();
    }

    @Override
    public DhPageInfo<highClassExchangeDaysVO> selectHighClassExchangeDaysList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder,Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<highClassExchangeDaysVO> retustList=productExchangeDbService.selectHighClassExchangeDaysList(order.toString(),enableDisable);
        PageInfo<highClassExchangeDaysVO> page =new PageInfo(retustList);
        return new DhPageInfo<highClassExchangeDaysVO>(page,retustList);
    }

    @Override
    public Boolean addHighClassExchangeDays(Integer exchangeDays, String productName, String productCode, String finishOrderProductNum,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        highClassExchangeDaysVO vo = new highClassExchangeDaysVO();
        vo.setHighClassExchangeFateId(guid);
        vo.setRuleNumber(ruleNumber);
        vo.setExchangeDays(exchangeDays);
        vo.setProductName(productName);
        vo.setCreateTm(date);
        vo.setEnableDisable(1);
        vo.setProductCode(productCode);
        vo.setFinishOrderProductNum(finishOrderProductNum);
        vo.setRuleTitle(ruleTitle);
        return productExchangeDbService.addHighClassExchangeDays(vo);
    }

    @Override
    public DhPageInfo<siteExchangeIndustryVO> selectSiteExchangeIndustryList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<siteExchangeIndustryVO> retustList=productExchangeDbService.selectSiteExchangeIndustryList(order.toString(),enableDisable);
        PageInfo<siteExchangeIndustryVO> page =new PageInfo(retustList);
        return new DhPageInfo<siteExchangeIndustryVO>(page,retustList);
    }

    @Override
    public Boolean addSiteExchangeIndustry(Integer siteDays, Integer industryNumber,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        siteExchangeIndustryVO vo = new siteExchangeIndustryVO();
        vo.setSiteExchangeIndustryId(guid);
        vo.setRuleNumber(ruleNumber);
        vo.setSiteDays(siteDays);
        vo.setIndustryNumber(industryNumber);
        vo.setCreateTm(date);
        vo.setEnableDisable(1);
        vo.setRuleTitle(ruleTitle);
        return productExchangeDbService.addSiteExchangeIndustry(vo);
    }

    @Override
    public DhPageInfo<industryExchangeSiteVO> selectIndustryExchangeSiteList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, Integer enableDisable) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<industryExchangeSiteVO> retustList=productExchangeDbService.selectIndustryExchangeSiteList(order.toString(),enableDisable);
        PageInfo<industryExchangeSiteVO> page =new PageInfo(retustList);
        return new DhPageInfo<industryExchangeSiteVO>(page,retustList);
    }

    @Override
    public Boolean addIndustryExchangeSite(Integer siteDays, Integer industryNumber,String ruleTitle) {
        //获取当前时间
        Date date = new Date();
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //给规则编码生成随机数
        String ruleNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));

        industryExchangeSiteVO vo = new industryExchangeSiteVO();
        vo.setIndustryExchangeSiteId(guid);
        vo.setRuleNumber(ruleNumber);
        vo.setSiteDays(siteDays);
        vo.setIndustryNumber(industryNumber);
        vo.setCreateTm(date);
        vo.setEnableDisable(1);
        vo.setRuleTitle(ruleTitle);
        return productExchangeDbService.addIndustryExchangeSite(vo);
    }

    @Override
    public DhPageInfo<CustomerMemberProductVO> selectHighCustomerMemberProductList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String productName) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<CustomerMemberProductVO> retustList=productExchangeDbService.selectHighCustomerMemberProductList(order.toString(),productName);
        PageInfo<CustomerMemberProductVO> page =new PageInfo(retustList);
        return new DhPageInfo<CustomerMemberProductVO>(page,retustList);
    }

}
